/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.cms.entity.ClassifiedInfo;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.service.*;
import com.thinkgem.jeesite.modules.cms.utils.CmsUtils;
import com.thinkgem.jeesite.modules.cms.utils.TplUtils;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章Controller
 * @author ThinkGem
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/classifiedInfo")
public class ClassifiedInfoController extends BaseController {

	@Autowired
	private ClassifiedInfoService classifiedInfoService;
	@Autowired
	private ClassifiedInfoDataService classifiedInfoDataService;
	@Autowired
	private CategoryService categoryService;
    @Autowired
   	private FileTplService fileTplService;
    @Autowired
   	private SiteService siteService;

    @Autowired
    private OfficeService officeService;
	
	@ModelAttribute
	public ClassifiedInfo get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return classifiedInfoService.get(id);
		}else{
			return new ClassifiedInfo();
		}
	}
	
	@RequiresPermissions("cms:classifiedInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClassifiedInfo classifiedInfo, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<ClassifiedInfo> page = classifiedInfoService.findPage(new Page<ClassifiedInfo>(request, response), classifiedInfo, true); 
        model.addAttribute("page", page);
		return "modules/cms/classifiedInfoList";
	}

	@RequiresPermissions("cms:classifiedInfo:view")
	@RequestMapping(value = "form")
	public String form(ClassifiedInfo classifiedInfo, Model model) {
		// 如果当前传参有子节点，则选择取消传参选择
		if (classifiedInfo.getCategory()!=null && StringUtils.isNotBlank(classifiedInfo.getCategory().getId())){
			List<Category> list = categoryService.findByParentId(classifiedInfo.getCategory().getId(), Site.getCurrentSiteId());
			if (list.size() > 0){
				classifiedInfo.setCategory(null);
			}else{
				classifiedInfo.setCategory(categoryService.get(classifiedInfo.getCategory().getId()));
			}
		}
        if(classifiedInfo.getOffice()!=null && StringUtils.isNoneBlank(classifiedInfo.getOffice().getId())){
            classifiedInfo.setOffice(officeService.get(classifiedInfo.getOffice().getId()));
        }
		classifiedInfo.setClassifiedInfoData(classifiedInfoDataService.get(classifiedInfo.getId()));
        model.addAttribute("contentViewList",getTplContent());
        model.addAttribute("article_DEFAULT_TEMPLATE",ClassifiedInfo.DEFAULT_TEMPLATE);
		model.addAttribute("classifiedInfo", classifiedInfo);
		CmsUtils.addViewConfigAttribute(model, classifiedInfo.getCategory());
		return "modules/cms/classifiedInfoForm";
	}

	@RequiresPermissions("cms:classifiedInfo:edit")
	@RequestMapping(value = "save")
	public String save(ClassifiedInfo classifiedInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, classifiedInfo)){
			return form(classifiedInfo, model);
		}
		classifiedInfoService.save(classifiedInfo);
		addMessage(redirectAttributes, "保存文章'" + StringUtils.abbr(classifiedInfo.getTitle(),50) + "'成功");
		String categoryId = classifiedInfo.getCategory()!=null?classifiedInfo.getCategory().getId():null;
        String officeId = classifiedInfo.getOffice()!=null?classifiedInfo.getOffice().getId():null;
		return "redirect:" + adminPath + "/cms/classifiedInfo/?repage&category.id="+(categoryId!=null?categoryId:"");
	}
	
	@RequiresPermissions("cms:classifiedInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(ClassifiedInfo classifiedInfo, String categoryId, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		// 如果没有审核权限，则不允许删除或发布。
		if (!UserUtils.getSubject().isPermitted("cms:classifiedInfo:audit")){
			addMessage(redirectAttributes, "你没有删除或发布权限");
		}
		classifiedInfoService.delete(classifiedInfo, isRe);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"文章成功");
		return "redirect:" + adminPath + "/cms/classifiedInfo/?repage&category.id="+(categoryId!=null?categoryId:"");
	}

	/**
	 * 文章选择列表
	 */
	@RequiresPermissions("cms:classifiedInfo:view")
	@RequestMapping(value = "selectList")
	public String selectList(ClassifiedInfo classifiedInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        list(classifiedInfo, request, response, model);
		return "modules/cms/articleSelectList";
	}
	
	/**
	 * 通过编号获取文章标题
	 */
	@RequiresPermissions("cms:classifiedInfo:view")
	@ResponseBody
	@RequestMapping(value = "findByIds")
	public String findByIds(String ids) {
		List<Object[]> list = classifiedInfoService.findByIds(ids);
		return JsonMapper.nonDefaultMapper().toJson(list);
	}

    private List<String> getTplContent() {
   		List<String> tplList = fileTplService.getNameListByPrefix(siteService.get(Site.getCurrentSiteId()).getSolutionPath());
   		tplList = TplUtils.tplTrim(tplList, ClassifiedInfo.DEFAULT_TEMPLATE, "");
   		return tplList;
   	}
}
